#!/usr/bin/env -S guile -e main -s
!#
(add-to-load-path (dirname (current-filename)))
(use-modules (ice-9 getopt-long)
             (srfi srfi-1)
             (grump compression)
             (grump utils))

(define (print-help args)
  (display (string-append "\
usage: " (car args) " [options] [input] [output]

options:
  -h, --help       Display this help
  -d, --debug      Print debug logs to stderr

Parse bencoded data from input (default stdin) and write the parsed
s-exp to output (default stdout).
")))

(define (main args)
  (let* ((option-spec '((help (single-char #\h) (value #f))
                        (debug (single-char #\d) (value #f))))
         (options       (getopt-long args option-spec))
         (help-wanted   (option-ref options 'help #f))
         (debug-wanted  (option-ref options 'debug #f))
         (extra-args    (option-ref options '() '())))
    (if help-wanted
        (print-help args)
        (begin
          (when debug-wanted
            (fluid-set! %grump-debug-enabled #t)
            (fluid-set! %grump-debug-to-error #t))
          (run extra-args)))))

(define (run args)
  (for-each (lambda (f)
              (display f)
              (newline))
            (filter (negate already-extracted?) args)))

(define (already-extracted? file)
  (any (compose file-exists? 7z-entry-name)
       (list-7z file)))
